home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / batch / batcher.c next >
C/C++ Source or Header  |  1989-06-27  |  3KB  |  132 lines

  1. /*
  2.  * batcher - send a bunch of news articles as an unbatch script
  3.  *
  4.  * Usage: batcher [-d dir] listfile
  5.  *
  6.  *    where listfile is a file containing a list, one per line, of
  7.  *    full pathnames of files containing articles.  Only the first
  8.  *    field of each line is looked at, so there can be more if needed
  9.  *    for other things.
  10.  *
  11.  *    The -d option specifies a directory where most articles are
  12.  *    likely to be; the program chdirs there to speed things up.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include "fgetmfs.h"
  21.  
  22. #ifndef READSIZE
  23. #define READSIZE 8192    /* allows for even 4.2 worst case file systems */
  24. #endif
  25. char buffer[READSIZE];
  26.  
  27. char *progname;
  28.  
  29. char *dir = NULL;        /* NULL means don't bother chdiring. */
  30. int dirlen;            /* strlen(dir) */
  31. int debug = 0;            /* Debugging? */
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     int c;
  38.     int errflg = 0;
  39.     extern int optind;
  40.     extern char *optarg;
  41.     register FILE *list;
  42.     char *article;
  43.     int ret;
  44.  
  45.     progname = argv[0];
  46.     while ((c = getopt(argc, argv, "d:x")) != EOF)
  47.         switch (c) {
  48.         case 'd':    /* Directory containing many articles. */
  49.             dir = optarg;
  50.             dirlen = strlen(dir);
  51.             break;
  52.         case 'x':    /* Debugging. */
  53.             debug++;
  54.             break;
  55.         case '?':
  56.         default:
  57.             errflg++;
  58.             break;
  59.         }
  60.     if (errflg || optind != argc-1) {
  61.         (void) fprintf(stderr,
  62.             "Usage: batcher [-d dir] listfile\n");
  63.         exit(2);
  64.     }
  65.  
  66.     list = fopen(argv[optind], "r");
  67.     if (list == NULL)
  68.         error("unable to open `%s'", argv[optind]);
  69.  
  70.     if (dir != NULL)
  71.         if (chdir(dir) < 0)
  72.             error("can't chdir to `%s'", dir);
  73.  
  74.     while ((article = fgetms(list)) != NULL) {
  75.         process(article);
  76.         free(article);
  77.     }
  78.     if (!feof(list))
  79.         error("fgetmfs failure", "");
  80.  
  81.     exit(0);
  82. }
  83.  
  84. /*
  85.  - process - process an article
  86.  */
  87. process(article)
  88. char *article;
  89. {
  90.     char *p;
  91.     register int artfile;
  92.     register int count;
  93.     struct stat sbuf;
  94.  
  95.     *(article + strcspn(article, "\n\t ")) = '\0';
  96.     if (dir != NULL && strncmp(article, dir, dirlen) == 0 &&
  97.             article[dirlen] == '/')
  98.         p = article+dirlen+1;
  99.     else
  100.         p = article;
  101.  
  102.     artfile = open(p, 0);
  103.     if (artfile < 0) {
  104.         /*
  105.          * Can't read the article.  This isn't necessarily a
  106.          * disaster, since things like cancellations will do
  107.          * this.  Mumble and carry on.
  108.          */
  109.         if (debug)
  110.             warning("can't find `%s'", p);
  111.         return;
  112.     }
  113.  
  114.     if (fstat(artfile, &sbuf) < 0)
  115.         error("internal disaster, can't fstat", "");
  116.     if ((sbuf.st_mode&S_IFMT) != S_IFREG) {
  117.         close(artfile);
  118.         return;        /* Don't try to batch directories etc. */
  119.     }
  120.  
  121.     (void) printf("#! rnews %ld\n", sbuf.st_size);
  122.     fflush(stdout);
  123.  
  124.     while ((count = read(artfile, buffer, sizeof buffer)) > 0)
  125.         if (write(1, buffer, count) != count)
  126.             error("write failure in `%s'", article);
  127.     if (count < 0)
  128.         error("read failure in `%s'", article);
  129.  
  130.     (void) close(artfile);
  131. }
  132.